package chat.server;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import chat.user.*;

public class ServerThread extends Thread {
	
	private Socket socket;
	private ObjectInputStream ois;
	private Object input;
	//private GroupProcessor group_processor;
	
	//private SocketProcessor socket_processor;
	private Message n;
	
	public ServerThread(Socket s) throws IOException{
		
		socket = s;
		//group_processor = groups;
	//	socket_processor = proc;
		
		start();
	}
	@Override
	public void run(){
		
		try {
			
			while (true){
				
				ois = new ObjectInputStream(new BufferedInputStream(socket.getInputStream()));
				
				Message m = (Message) ois.readObject();
				
				if (m.AUTH_REQUEST){
					
					GroupProcessor.registerNewUser(socket, m.getUser());
					
					MessageCreator.AuthRequestResponse(socket);
					
					SocketProcessor.addMessageOutQueue(MessageCreator.AuthRequestResponse(socket));
				}
				
				MultiThread.addInMessage(m);
				
				System.out.println();
			}
		}
		
		catch(IOException e){
				
			e.printStackTrace();
			
		} 
		catch (ClassNotFoundException e) {
			
			e.printStackTrace();
		}
		
	}
	
	public Socket getSocket(){
		return socket;
	}
}
